#include "tcp_client.h"

void Client::start()
{
    if(connect(fd, (struct sockaddr*)&addr, sizeof(addr)) == -1)
    {
        cerr << "connect failed" << endl;
        return;
    }

    bool flag = true;
    char str[BUFFER_SIZE];
    while(flag)
    {
        cin >> str;
        sendMessage(fd, str);
        cout << "client send successful" << endl;
        string message = receiveMessage(fd);
        if(message.size() > 0)
        {
            cout << "【server】# " << message << endl;
        }
        else if(message.size() == 0) 
        {
            cout << "server closed the connection" << endl;
            flag = false;
        }
        else 
        {
            cerr << "recv failed" << endl;
            return;
        }
    }
}